<template>
  <div class="com-container">
    <div class="com-chart" ref="stock_ref"></div>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  name: "Stock",

  data() {
    return {
      chartInstance: null,
      allData: null,
      currentIndex: 0, //当前显示
      timeId: null,
    };
  },
computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme(){
      this.chartInstance.dispose()
      this.initChart();
      this.screenAdapter()
      this.updateCharts()
    }
  },
  mounted() {
    this.initChart();
    this.getData();
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },

  destroyed() {
    window.removeEventListener("resize", this.screenUpdate);
    clearInterval(this.timeId);
  },
  methods: {
    // 初始化
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.stock_ref, this.theme);
      const initOption = {
        title: {
          text: '▎ '+"库存销售量",
          top: 20,
          left: 20,
        },
      };
      this.chartInstance.setOption(initOption);
      this.chartInstance.on("mouseover", () => {
        clearInterval(this.timeId);
      });
      this.chartInstance.on("mouseout", () => {
        this.startSetInterVal();
      });
    },
    // 获取数据
    async getData() {
      let { data: res } = await this.$http.get("stock");
      this.allData = res;
      this.updateChart();
      this.startSetInterVal();
    },
    // 更新图表
    updateChart() {
      const centerArr = [
        ["18%", "40%"],
        ["50%", "40%"],
        ["82%", "40%"],
        ["34%", "75%"],
        ["66%", "75%"],
      ];
      const colorArr = [
        ["#4FF778", "#0BA82C"],
        ["#E5DD45", "#E8B11C"],
        ["#E8821C", "#E55445"],
        ["#5052EE", "#AB6EE5"],
        ["#23E5E5", "#2E72BF"],
      ];
      const start = this.currentIndex * 5;
      const end = (this.currentIndex + 1) * 5;
      let showData = this.allData.slice(start, end);
      let sericeArr = showData.map((item, index) => {
        return {
          type: "pie",
          // radius: [110, 100],
          center: centerArr[index],
          avoidLabelOverlap: false,
          // 关闭输入移入饼图时的动画效果
          hoverAnimation: false,
          labelLine: {
            show: false,
          },
          label: {
            position: "center",
            color: colorArr[index][0],
          },
          data: [
            {
              name: item.name + "\n" + item.sales,
              value: item.sales,
              itemStyle: {
                color: new this.$echarts.graphic.LinearGradient(0, 1, 0, 0, [
                  {
                    offset: 0,
                    color: colorArr[index][0],
                  },
                  {
                    offset: 1,
                    color: colorArr[index][1],
                  },
                ]),
              },
            },
            {
              value: item.stock,
              itemStyle: {
                color: "#333843",
              },
            },
          ],
        };
      });
      const updateOption = {
        series: sericeArr,
      };
      this.chartInstance.setOption(updateOption);
    },
    // 分辨率调整
    screenAdapter() {
      const titleFontSize  = (this.$refs.stock_ref.offsetWidth / 100) * 3.6;
      const innerRadius = titleFontSize * 2.8;
      const outterRadius = innerRadius * 1.125;

      let config = {
        type: "pie",
        radius: [outterRadius, innerRadius],
        label: {
          fontSize: titleFontSize / 2,
        },
      };
      let ret = [];
      for (let index = 0; index < 5; index++) {
        ret.push(config);
      }
      const adapterOption = {
        series:ret
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },
    //定时器
    startSetInterVal() {
      if (this.timeId) {
        clearInterval(this.timeId);
      }
      this.timeId = setInterval(() => {
        this.currentIndex++;
        if (this.currentIndex > Math.ceil(this.allData.length / 5) - 1) {
          this.currentIndex = 0;
        }
        this.updateChart();
      }, 5000);
    },
  },
};
</script>

<style lang="less" scoped></style>
